package com.jy.api.controller.member;

import com.jy.api.dao.MemberDao;
import com.jy.api.dao.OrderDao;
import com.jy.api.dao.StoreDao;
import com.jy.api.domain.Member;
import com.jy.api.domain.Order;
import com.jy.api.domain.Store;
import com.jy.api.service.OrderService;
import com.jy.api.util.ISyncExecutor;
import com.jy.api.util.RedisSyncLocker;
import com.jy.api.vo.pos.order.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by lihu on 2017/11/20.
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1/member", produces = "application/json;charset=utf-8")
public class MemberOrderController {

    @Autowired
    MemberDao memberDao;

    @Autowired
    OrderService orderService;

    @Autowired
    OrderDao orderDao;

    @Autowired
    StoreDao storeDao;
    @Autowired
    private RedisSyncLocker redisSyncLocker;

    /**
     * 获取会员消费记录
     *
     * @param openid
     * @param pageable
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{openid}/orders")
    public ResponseEntity getMemberRecord(@PathVariable String openid,
                                          @PageableDefault(value = 10, sort = {"paymentTime"}, direction = Sort.Direction.DESC) Pageable pageable) {
        try {
            Member member = memberDao.findByOpenId(openid);
            if (member == null) {
                return new ResponseEntity("你不是店铺会员！请先成为会员！", HttpStatus.BAD_REQUEST);
            }

            return ResponseEntity.ok(orderService.getMemberRecord(member.getId(), pageable));

        } catch (Exception ex) {
            log.error("getMemberRecord is error==> openid:{},", openid, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 订单明细
     *
     * @param orderId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "{orderId}/order-info")
    public ResponseEntity getOrderInfo(@PathVariable String orderId) {
        try {
            return (ResponseEntity) redisSyncLocker.idLock(orderId, o -> {
                Order order = orderDao.findOne(orderId);
                if (order == null) {
                    return new ResponseEntity("查询失败！订单信息不存在！", HttpStatus.BAD_REQUEST);
                }
                Store store = storeDao.findOne(order.getStoreId());
                OrderVo orderVo = orderService.orderVoCouponInstance(order);
                orderVo.setStoreName(store.getName());
                orderVo.setStoreAddress(store.getAddr());
                return ResponseEntity.ok(orderVo);
            });
        } catch (Exception ex) {

            log.error("getChargeRules is error=>orderId:{}", orderId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

}

